function [ index ] = findFirstAfter( img, tr, br, lc, rc, after, dim, bgOrTxt )
    index = -1;
    %Check the value of after
    if dim == 1 
        %Check the columns
        if after > rc || after < lc
            return;
        else
            %Shrink the image
            I = img(tr:br,after:rc);
            if bgOrTxt == 0
                %Looking for text
                index = min(find(any(I==0,dim)));
            elseif bgOrTxt == 1
                %Looking for background
                index = min(find(all(I==1,dim)));
            end
        end
    elseif dim == 2
        %Check the rows
        if after > br || after < tr
            return;
        else
            %Shrink the image
            I = img(after:br,lc:rc);
            if bgOrTxt == 0
                %Looking for text
                index = min(find(any(I==0,dim)));
            elseif bgOrTxt == 1
                %Looking for background
                index = min(find(all(I==1,dim)));
            end
        end
    end
    if isempty(index)
        index = - 1;
    else
        index = index + after - 1;
    end
    
end

